<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2017/1/5
 * Time: 13:06
 */

namespace Home\Controller;


use Common\Model\CommodityModel;
use Org\Util\Cart;

class CartController extends HomeQController
{
    public function index(){
        $conf=[
            'js'=>['list']
        ];
        $this->assign('conf',$conf);
        $confData=[
            'css'=>['account']
        ];
        $this->assign('confData',$confData);
        $data=$_SESSION['cart'] ? $_SESSION['cart'] : [];
        //循环压入图片
        foreach($data['goods'] as $k=>$v){
            $data['goods'][$k]['pic']=(new CommodityModel())->where("gid={$v['id']}")->getField('pic');
        }
        $this->assign('data',$data);
//        dd($data);
//        dd($_SESSION);
        //显示模板
        $this->display();
    }
    /**
     * 异步修改购物车数据
     */
    public function ajaxUpdateCart()
    {
        if(IS_AJAX){
//            dd($_POST);
            $data=array(
                'sid'=>$_POST['sid'],// 唯一 sid，添加购物车时自动生成
                'num'=>$_POST['num']
            );
            (new Cart())->update($data);
        }
    }
    /**
     * 异步删除购物车商品
     */
    public function ajaxDelCart(){
        if(IS_AJAX){
            $data=$_POST['sid'];
//            $data=array(
//                'sid'=>$_POST['sid']
//            );
            (new Cart())->del($data);
        }
    }
}